package com.etop.learnapp.fragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.etop.learnapp.R;
import com.etop.learnapp.activity.ApplyActivity;
import com.etop.learnapp.personalClass.Student;

import java.util.ArrayList;


public class Student_Info_Fragment extends Fragment {
    View view;
    Button btnComplete_Edit2,btn_studentInfoBack;
    ApplyActivity applyActivity;
    private ArrayList<Student> studentArrayList;
    private EditText etStudentName, etStudentSex, etStudentMode, etStudentSoftware_2D, etStudentSoftware_3D, etStudentID, etStudentRemarks;
    int position;

    public void setPosition(int position) {
        this.position = position;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        applyActivity = (ApplyActivity) this.getActivity();
        studentArrayList = applyActivity.studentArrayList;
    }

    public void init(View view) {
        btnComplete_Edit2 = (Button) view.findViewById(R.id.btn_complete_Edit2);
        btn_studentInfoBack = (Button)view.findViewById(R.id.btn_studentInfoBack);
        btn_studentInfoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                applyActivity.setCurrentPage(3);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_Main, applyActivity.student_list_fragment);
                fragmentTransaction.addToBackStack(null);//
                fragmentTransaction.commit();
            }
        });

        etStudentName = (EditText) view.findViewById(R.id.etStudentName);
        etStudentSex = (EditText) view.findViewById(R.id.etStudentSex);
        etStudentMode = (EditText) view.findViewById(R.id.etStudentMode);
        etStudentSoftware_2D = (EditText) view.findViewById(R.id.etStudentSoftware_2D);
        etStudentSoftware_3D = (EditText) view.findViewById(R.id.etStudentSoftware_3D);
        etStudentID = (EditText) view.findViewById(R.id.etStudentID);
        etStudentRemarks = (EditText) view.findViewById(R.id.etStudentRemarks);
    }


    private void show(){
        if (studentArrayList.get(position) != null){

            etStudentName.setText(studentArrayList.get(position).getName().toString());
            etStudentSex.setText(studentArrayList.get(position).getSex().toString());
            etStudentMode.setText(studentArrayList.get(position).getMode().toString());
            etStudentSoftware_2D.setText(studentArrayList.get(position).getSoftware_2D().toString());
            etStudentSoftware_3D.setText(studentArrayList.get(position).getSoftware_3D().toString());
            etStudentID.setText(studentArrayList.get(position).getID().toString());
            etStudentRemarks.setText(studentArrayList.get(position).getRemarks().toString());
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_student__info, container, false);
        init(view);
        show();

        btnComplete_Edit2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 {
                    if (studentArrayList.get(position) == null) applyActivity.ExtendStudentList();

                     studentArrayList.get(position).setName(etStudentName.getText().toString());
                     studentArrayList.get(position).setSex(etStudentSex.getText().toString());
                     studentArrayList.get(position).setMode(etStudentMode.getText().toString());
                     studentArrayList.get(position).setSoftware_2D(etStudentSoftware_2D.getText().toString());
                     studentArrayList.get(position).setSoftware_3D(etStudentSoftware_3D.getText().toString());
                     studentArrayList.get(position).setID(etStudentID.getText().toString());
                     studentArrayList.get(position).setRemarks(etStudentRemarks.getText().toString());
                }

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_Main, ((ApplyActivity)getActivity()).student_list_fragment);
                fragmentTransaction.commit();
            }
        });

        return view;
    }

}
